package com.doit.day01;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;


public class WatchNode1 {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("linux01:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("没有任何业务意义，仅仅是开启一个监听");
            }
        });

        //监听/doit这个节点值的变化
        zk.getChildren("/doit", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                //如果子节点的个数发生了变化，那么，我可以做一些具体的业务逻辑
                try {
                    List<String> children = zk.getChildren("/doit", this);
                    System.out.println("改变之后子节点的情况是："+children);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        //休眠
        Thread.sleep(Long.MAX_VALUE);
        zk.close();

    }
}
